package crm.controller;


import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.beans.PropertyChangeListener;
import java.sql.SQLException;
import java.text.DecimalFormat;

import javax.swing.AbstractAction;
import javax.swing.Action;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTable;
import javax.swing.SpringLayout;
import javax.swing.table.DefaultTableModel;

import crm.daoInterfaces.CustomerInfoDao;
import crm.daoInterfaces.PriorityDao;
import crm.daoInterfaces.SalesPersonDao;
import crm.dto.Leads;
import crm.dto.Priority;
import crm.dto.SalesPerson;
import crm.implementor.CustomerInfoImp;
import crm.implementor.LeadsImp;
import crm.implementor.PriorityImp;
import crm.implementor.SalesPersonImp;
import crm.model.FollowUpAction;
import crm.utility.AppUtility;
import crm.utility.ButtonColumn;
import crm.utility.ViewUtilites;
import crm.view.leeds.FollowupActionsView;
import crm.view.leeds.LeadHomeView;
import crm.viewframework.UIFrameWork;
import crm.viewframework.Validater;





@SuppressWarnings("unused")
public class LeadHomeController implements ActionListener
{
	LeadHomeView view;
	Leads model;
	Leads[] lead;
	
	
	public LeadHomeController() 
	{
		
	}


	public LeadHomeController(LeadHomeView view, Leads model) throws Exception 
	{
		super();
		this.view = view;
		this.model = model;
		view.actionListener(this);
		AppUtility.setComboAtten(view.getComboAttended());
		AppUtility.setComboPriority(view.getComboPriority());
		setGoToColumn();                // This new Method is responsible for adding go to button in last columns
	}


	
	
	
	
	
	public void reactOnApplyFilter() throws SQLException
	{
		String attendedCombo,priorityCombo,Todate,Fromdate;
		attendedCombo=view.getComboAttended().getSelectedItem().toString();
		priorityCombo=view.getComboPriority().getSelectedItem().toString();
		
		String message = new Validater(view).validateForEmpty();
		Todate=view.getLblTo().getText();
	    Fromdate=view.getLblFrom().getText();
	    
	    java.sql.Date fdate = null;
	    java.sql.Date tdate = null;
		try {
			fdate = ViewUtilites.convert(Fromdate);
			tdate=ViewUtilites.convert(Todate);
		} catch (Exception e) {
			JOptionPane.showMessageDialog(null,"Invalid Dates!");
			e.printStackTrace();
			return;
			
		}
		
	    boolean dateCompare= fdate.after(tdate)||fdate.equals(tdate);
		if(message.equals("")&&!Todate.equals("TO")&&!Fromdate.equals("FROM")&& dateCompare)
		{
			lead=LeadsImp.getLeadsByFilter(fdate, tdate, attendedCombo, priorityCombo);
			DefaultTableModel dm=(DefaultTableModel) view.getTable().getModel();
		
			// Check the length of lead array.If it is zero it means no elements are returned.
			System.out.println("Lead Length:"+lead.length);
			if(lead.length==0)
			    JOptionPane.showMessageDialog(view,"No Data Available For Given Criteria !");
		
			for(int k=dm.getRowCount()-1;k>-1;k--)
			dm.removeRow(k);
		
			for(int i=0;i<lead.length;i++)
				{
			
			if(lead[i]==null)
				;
			
			else
			{
				// Format the double for phone
				DecimalFormat df = new DecimalFormat("#"); 
				String phone =df.format(lead[i].getModelphone());
				dm.addRow(new Object[]{lead[i].getModelname(),phone,lead[i].getEnquiryDate(),lead[i].getMin_budget(),lead[i].getMax_budget()
				           ,lead[i].getModelproductName(),lead[i].getPriorityName(),false,new Object()});
		     
			}
		
				
		}	
	
	}	
		
		else
		{
			
			if(dateCompare==false)
				message = message+"From Date is Smaller Then To Date";
			JOptionPane.showMessageDialog(view,message);
		}
	
	}
	
	@SuppressWarnings("serial")
	private void setGoToColumn()
	{
		Action delete = new AbstractAction()
		{
		    JTable table=view.getTable();
			public void actionPerformed(ActionEvent e)
		    {
		       
		    	table = (JTable)e.getSource();
		        int modelRow = Integer.valueOf( e.getActionCommand() );
		       
		        SpringLayout layout=(SpringLayout) view.getParent().getLayout();
		        JPanel panel=(JPanel) view.getParent();
		        FollowupActionsView followup=new FollowupActionsView();
		        FollowUpActionsController controller=new FollowUpActionsController(followup,lead[modelRow]);
		        
		        UIFrameWork.setReportComponent(layout,followup,panel,2);
		    }
		};

		ButtonColumn buttonColumn = new ButtonColumn(view.getTable(),delete,7);
		buttonColumn.setMnemonic(KeyEvent.VK_D);
	}


	@Override
	public void actionPerformed(ActionEvent arg0) 
	{
		String k=arg0.getActionCommand();
		if(k.contains("APPLY FILTER"))
		{
			try {
				reactOnApplyFilter();
			} catch (SQLException e) {
			
				//e.printStackTrace();
			}
		}
		if(k.contains("SEND MAIL"))
		{
			
		}
		if(k.contains("SEND MESSAGE"))
		{
			
		}
		if(k.equals("Cl1"))
		{
			
			ViewUtilites.makeDateComponent(view.getLblTo());
		}
		if(k.equals("Cl"))
		{
			
			ViewUtilites.makeDateComponent(view.getLblFrom());
		}
		
	}


	
	
	

}
